package com.fzy.core.system.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.fzy.core.system.entity.EntityWrapper;
import com.fzy.core.system.entity.PropertyWrapper;
import com.fzy.core.system.service.DbService;
import com.fzy.core.utils.BuilderUtils;
import com.fzy.core.validators.V;
import com.fzy.core.webwork.BaseAction;
import com.fzy.core.webwork.MyValidationAware;
import com.opensymphony.xwork.Preparable;

public class CommonSaveAction extends BaseAction implements Preparable, MyValidationAware {

	private static final long serialVersionUID = 9134988260301408386L;
	String errorDispatch;
	Map m;
	EntityWrapper entityWrapper;
	DbService dbService;
	boolean isCreated = false;
	
	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/WEB-INF/jsp/core/system/common-input.jsp";
		entityWrapper = dbService.initEntityWrapper();

		m = dbService.getEntityData(entityWrapper, request);
		if(m == null){
			isCreated = true;
			m = new HashMap();
		} 
	}
	
	public void validate() {
		for (Iterator iterator = entityWrapper.getProperties().iterator(); iterator.hasNext();) {
			PropertyWrapper pw = (PropertyWrapper) iterator.next();
			Object value = m.get(pw.getData().getNameText());
			Object propertyValue = BuilderUtils.params2javaType(value,
					pw.getJavaType());
			if(value != null){
				String[] x = (String[]) value;
				if(x.length == 1) value = x[0];
			}
			
			String valueString = (String)value;
			String javaType = pw.getJavaType();
			String fieldName = "m." + pw.getNameUncapital();
			String fieldNameShow = pw.getData().getNameShow();
			
			if(PropertyWrapper.JAVA_LONG.equals(javaType)){
				if(!V.integer(valueString)){
			        String message = getText("error.integer", new String[]{fieldNameShow});
			        addFieldError(fieldName, message);
				}
			}
			if(PropertyWrapper.JAVA_DOUBLE.equals(javaType)){
				if(!V.number(valueString)){
			        String message = getText("error.number", new String[]{fieldNameShow});
			        addFieldError(fieldName, message);
				}
			}
			if(PropertyWrapper.JAVA_DATE.equals(javaType) || pw.getHasDateFormatShow()){
				String pattern = pw.getDateFormatShow();
				if(!V.matchDate(valueString, pattern)){
			        String message = getText("error.date", new String[]{fieldNameShow, pattern});
			        addFieldError(fieldName, message);
				}
			}
			if(pw.getIsNotNull()){
				if(!V.required(propertyValue)){
			        String message = getText("error.required", new String[]{fieldNameShow});
			        addFieldError(fieldName, message);
				}
			}
			if(PropertyWrapper.JAVA_STRING.equals(javaType)){
				int maxLength = pw.getData().getLengthValue().intValue();
				if(!V.maxLength(valueString, maxLength)){
			        String message = getText("error.maxLength", new String[]{fieldNameShow, maxLength + ""});
			        addFieldError(fieldName, message);
				}
			}
		}
	}

	public String execute() throws Exception {
		dbService.saveEntityData(entityWrapper, m, isCreated);
		
		addActionMessage(getText("success.save"));
		return "dispatcher:/pages/refresh-parent.jsp";
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}

	public Map getM() {
		return m;
	}

	public EntityWrapper getEntityWrapper() {
		return entityWrapper;
	}
	
}
